package gov.mogaha.ntis.cmm.mob.util;

import java.util.Timer;

import android.content.Context;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;


public class GpsUtil {
	Context ctx=null;
    Timer timer1=null;
    LocationManager lm;
    LocationResult locationResult;
    boolean mGpsEnabled=false;
    boolean mNetworkEnabled=false;

    public GpsUtil(Context context, LocationResult result) {
    	Logger.i("GpsUtil Run**************************");
    	ctx = context;
    	locationResult=result;
    	settingGps();
    }
    public void clear() {
    	lm.removeUpdates(locationListenerGps);
    	lm.removeUpdates(locationListenerNetwork);
    }
    public void reset() {
    	lm.removeUpdates(locationListenerGps);
    	lm.removeUpdates(locationListenerNetwork);
    	settingGps();
    }
    private boolean settingGps () {
    	if(lm==null)
            lm = (LocationManager) ctx.getSystemService(Context.LOCATION_SERVICE);
        
        //exceptions will be thrown if provider is not permitted.
        try{mGpsEnabled=lm.isProviderEnabled(LocationManager.GPS_PROVIDER);}catch(Exception ex){Logger.w(ex.toString());}
        try{mNetworkEnabled=lm.isProviderEnabled(LocationManager.NETWORK_PROVIDER);}catch(Exception ex){Logger.w(ex.toString());}

        Logger.i("gps_enabled ==>"+mGpsEnabled+" network_enabled ==>"+mNetworkEnabled);
        
        //don't start listeners if no provider is enabled
        if(!mGpsEnabled && !mNetworkEnabled)
            return false;

        if(mGpsEnabled)
            lm.requestLocationUpdates(LocationManager.GPS_PROVIDER, 1000, 5, locationListenerGps);
        if(mNetworkEnabled)
            lm.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 1000, 5, locationListenerNetwork);
       
        return true;
    }
    public boolean getIsProviderEnable(String provider) {
    	if ( provider == LocationManager.GPS_PROVIDER)
    		return mGpsEnabled;
    	else
    		return mNetworkEnabled;
    }
    public Location GetLastLocation() {
    	Location net_loc=null, gps_loc=null;
        
        if(mGpsEnabled)
            gps_loc=lm.getLastKnownLocation(LocationManager.GPS_PROVIDER);
        if(mNetworkEnabled)
            net_loc=lm.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);

        if(gps_loc!=null && net_loc!=null){
            if(gps_loc.getTime()>net_loc.getTime()) return gps_loc;
            else return net_loc;
        }
        if(gps_loc!=null) return gps_loc;
        if(net_loc!=null) return net_loc;
        
        return null;
    }
    LocationListener locationListenerGps = new LocationListener() {
        public void onLocationChanged(Location location) {
        	Logger.i("GPS Send===>>>"+location.toString());
        	lm.removeUpdates(locationListenerNetwork);
        	locationResult.gotLocation(location, LocationManager.GPS_PROVIDER);
        }
        public void onProviderDisabled(String provider) {
        	Logger.i(provider+"  **Disavble**");
        	lm.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 1000, 10, locationListenerNetwork);
        }
        public void onProviderEnabled(String provider) {
        	Logger.i(provider+"  **Enable**");
        	lm.removeUpdates(locationListenerNetwork);
        }
        public void onStatusChanged(String provider, int status, Bundle extras) {Logger.i(provider);}
    };

    LocationListener locationListenerNetwork = new LocationListener() {
        public void onLocationChanged(Location location) {
        	Logger.i("NetWork Send===>>>"+location.toString());
        	locationResult.gotLocation(location, LocationManager.NETWORK_PROVIDER);
        }
        public void onProviderDisabled(String provider) {
        	Logger.i(provider+"  **Disavble**");
        }
        public void onProviderEnabled(String provider) {
        	Logger.i(provider+"  **Enable**");
        }
       
        public void onStatusChanged(String provider, int status, Bundle extras) {Logger.i(provider);}
    };

    public static abstract class LocationResult{
        public abstract void gotLocation(Location location, String provider);
    }
}
